<?php
include_once('../koneksi/koneksi.php');
include_once('../kelas/record.php');
include_once('../kelas/kategori.php');
include_once('../kelas/produk.php');
include_once('../kelas/amankanParam.php');
include_once('../kelas/fungsi.php');
include_once('../kelas/kota.php');
$objParamAman = new amankanParam();
$dtPost = $objParamAman->postMethod();
$dtGet = $objParamAman->getMethod();
$dtPerPage = 10;
$start = isset($dtGet['hal']) ? $dtGet['hal'] : 0;
$dtKota = $objKota->getData($dtPerPage, $start * $dtPerPage);
?>
<h2>Biaya Pengiriman</h2>
<form name="formKota" method="post" action="<?php echo BASEURL . 'proses/kota.php?on=simpan'; ?>">
    <table>
        <tr>
            <td>Kota</td>
            <td><input name="kota" required="required" class="text_box_3" /><input type="hidden" name="id" /></td>
        </tr>
        <tr>
            <td>Biaya</td>
            <td><input  required="required" name="biaya" class="text_box_3" /></td>
        </tr>
        <tr>
            <td>Aktif</td>
            <td><input type="checkbox"   name="aktif" value="1" checked="checked" class="text_box_3" /></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center"><input type="submit" value="Simpan" /><input type="reset" value="Reset" /></td>
        </tr>
    </table>
</form>
<div class="frame_table_order">
<table width="100%" cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">

    <thead>
        <tr align="center" valign="middle" bgcolor="#CCCCCC">
            <th>No</th>
            <th>Kota</th>
            <th>Biaya</th>
            <th>Pilihan</th>
        </tr>
    </thead>
    <tbody>
        <?php
        if ($dtKota['total'] > 0) {
            $no = 1;
            foreach ($dtKota['result'] as $key => $value) {
                ?>
                  <tr class="font_isi_judul" align="center" valign="middle">
              <td><?php echo $no++; ?></td>
                    <td><?php echo $value['kota'];?></td>
                    <td><?php echo number_format($value['biaya'],0,'.',',');?></td>
                    <td>
                        <a class="option aktifkota"
                        aktif ='<?php echo (int)$value['aktif'];?>'
                        href="<?php echo $value['id'];?>"><?php echo ($value['aktif']==1)?"Nonaktifkan":"Aktifkan";?></a> 
                        <a class="option editkota"
                        aktif ='<?php echo (int)$value['aktif'];?>'
                        kota ='<?php echo $value['kota'];?>'
                        biaya ='<?php echo $value['biaya'];?>'
                        href="<?php echo $value['id'];?>">Edit</a> 
                    </td>
                </tr>
                <?php
            }
        } else {
            ?>
            <tr>
                <td colspan="4">(data tidak ada)</td>
            </tr>
            <?php
        }
        ?>
    </tbody>
    <tfoot></tfoot>
</table>
</div>
<script type="text/javascript">
$(document).ready(function(){
    $('.aktifkota').click(function(e){
        var idAktif= e.currentTarget.getAttribute('href');
        var statAktif = e.currentTarget.getAttribute('aktif')==true?0:1;
        var dtSend = {};
        if(statAktif==1){
            dtSend={
                id:idAktif,
                aktif:statAktif
            }
        }else{
            dtSend={
                id:idAktif
            }
        };
        $.ajax({
            url:baseUrl+'proses/kota.php?on=simpan',
            data:dtSend,
            type:'POST',
            dataType:'JSON',
            success:function(e){
                if(e.success==false){
                    alert(e.error_msg);
                }
                else{
                    document.location = document.URL;
                }
            },
            error:function(e){
                alert('Gagal Ubah Status Aktif');
            }
        });
        e.preventDefault();
    });
    $('.editkota').click(function(e){
        var kota = e.currentTarget.getAttribute('kota');
        var aktif = e.currentTarget.getAttribute('aktif');
        var biaya = e.currentTarget.getAttribute('biaya');
        var id = e.currentTarget.getAttribute('href');
        var formKota = $(document).find('form[name="formKota"]');
        formKota.find('input[name="id"]').val(id);
        formKota.find('input[name="kota"]').val(kota);
        formKota.find('input[name="biaya"]').val(biaya);
        formKota.find('input[type="checkbox"]').val(aktif);
        formKota.find('input[type="checkbox"]').removeAttr('checked');
        if(aktif==true|| aktif=="1"){
            formKota.find('input[type="checkbox"]').click();
        }
        e.preventDefault();
    });
    var formKota = $(document).find('form[name="formKota"]');
    formKota.submit(function(e){
        $.ajax({
            url:baseUrl+'proses/kota.php?on=simpan',
            dataType:'JSON',
            data:formKota.serialize(),
            type:'POST',
            success:function(e){
                if(e.success==false){
                    alert(e.error_msg);
                }
                else{
                   document.location = document.URL;
     }
 },
 error:function(e){
    console.log(e);
    alert('Gagal Simpan Ongkos Kirim');
}
})
        e.preventDefault();
    });
})
</script>